package cn.example.util.enumerate;

/**
 * 返回码
 * Created by WuYB on 2017/11/23.
 */
public enum ResultCode {
    /**
     * 成功
     */
    SUCCESS(1, "success"),
    /**
     * 失败（捕捉到异常）
     */
    ERROR(-1, "error"),
    /**
     * 校验失败
     */
    VALID_FAIL(-2, "valid fail"),
    /**
     * 数据库已存在
     */
    UNIQUE_FAIL(-3, "unique fail");

    private int code;

    private String msg;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    ResultCode(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public int value() {
        return this.code;
    }

    /**
     * 根据值，返回类型的枚举实例
     *
     * @param code 值名称
     */
    public static ResultCode valueOf(int code) {
        for (ResultCode type : ResultCode.values()) {
            if (type.getCode() == code) {
                return type;
            }
        }
        throw new IllegalArgumentException(
                "No enum code value " + code);
    }

    @Override
    public String toString() {
        return msg;
    }
}
